package itsm.isperp.module.controller.db;

import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.module.entity.db.DbEntityField;
import itsm.isperp.module.entity.db.DbEntityFieldControl;
import itsm.isperp.module.service.db.DbEntityFieldControlService;
import itsm.isperp.module.service.db.DbEntityFieldService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 实体字段控制器
 * 
 * @author lizx
 * @date 2013-5-5
 */
@Controller
@RequestMapping("db/entityFieldControl")
public class DbEntityFieldControlController extends
		BaseController<DbEntityFieldControl> {

	@Autowired
	DbEntityFieldService dbEntityFieldService;

	@Autowired
	DbEntityFieldControlService dbEntityFieldControlService;

	@Override
	public DbEntityFieldControlService getService() {
		return dbEntityFieldControlService;
	}

	@Override
	public String getPageTitle() {
		return "实体字段控件管理";
	}

	/**
	 * 编辑界面
	 * 
	 * @param id
	 *            唯一标识
	 * @return 编辑视图
	 */
	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {
		DbEntityField entityField = null;
		ModelAndView mv = new ModelAndView(this.getModulePath() + "/edit");
		DbEntityField model = null;
		if (StringUtils.isEmpty(id)) {
			String entityId = req.getParameter("entityId");
			if (StringUtils.isEmpty(entityId)) {
				return null;
			}

			entityField = dbEntityFieldService.findOne(entityId);
			model = new DbEntityField();
			model.setDbEntityId(entityId);

		} else {

			model = dbEntityFieldService.findOne(id);
			entityField = dbEntityFieldService.findOne(model.getId());
		}
		mv.addObject("pageTitle", this.getPageTitle());
		mv.addObject("model", model);

		mv.addObject("entityFieldName", entityField.getName());

		return mv;
	}

	@RequestMapping("listByEntityId")
	@ResponseBody
	public List<Map<String, Object>> listByEntityId(String entityId,
			String slmField) {

		return this.getService().listByEntityId(entityId, "1".equals(slmField));

	}

}
